package com.chx.three.mama.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chx.three.mama.R;
import com.chx.three.mama.model.RingBBS;
import com.chx.three.mama.utils.BitmapHelper;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

/**
 * 论坛普通帖子的adapter
 */
public class BbsCommonAdapter extends BaseAdapter {
    private Context mContext;
    private List<RingBBS> data;
    public BbsCommonAdapter(Context context,List<RingBBS> data){
        this.mContext=context;
        this.data=data;
    }
    @Override
    public int getCount() {
        if(data!=null){
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(data!=null){
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;
        if(convertView!=null){
            view=convertView;
        }else{
            view= LayoutInflater.from(mContext).inflate(R.layout.item_ring_bbs_common,parent,false);
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        if(holder==null){
            holder=new ViewHolder();
            holder.icon=(ImageView)view.findViewById(R.id.ring_bbs_common_icon);
            holder.jian=(ImageView)view.findViewById(R.id.ring_bbs_common_jian);
            holder.haspic=(ImageView)view.findViewById(R.id.ring_bbs_common_haspic);
            holder.title=(TextView)view.findViewById(R.id.ring_bbs_common_title);
            holder.nickName=(TextView)view.findViewById(R.id.ring_bbs_common_nickname);
            holder.time=(TextView)view.findViewById(R.id.ring_bbs_common_time);
            holder.reply=(TextView)view.findViewById(R.id.ring_bbs_common_reply);
            holder.state=(TextView)view.findViewById(R.id.ring_bbs_common_state);
            view.setTag(holder);
        }
        RingBBS bbs = data.get(position);
        //推荐
        int post_type = bbs.getPost_type();
        if(post_type==2){
            holder.jian.setVisibility(View.VISIBLE);
        }else{
            holder.jian.setVisibility(View.GONE);
        }
        //头像
        BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
        if(!TextUtils.isEmpty(bbs.getAvatar())){
            bitmapUtils.display(holder.icon,bbs.getAvatar());
        }
        //hasPic
        String pic = bbs.getPic();
        int anInt = Integer.parseInt(pic);
        if(anInt!=0){
            holder.haspic.setVisibility(View.VISIBLE);
        }else{
            holder.haspic.setVisibility(View.GONE);
        }
        //设置标题，昵称，访问量
        if(!TextUtils.isEmpty(bbs.getTitle())){
            holder.title.setText(bbs.getTitle());
        }
        if(!TextUtils.isEmpty(bbs.getNickname())){
            holder.nickName.setText(bbs.getNickname());
        }
        if(!TextUtils.isEmpty(bbs.getShowdated())){
            holder.time.setText(bbs.getShowdated());
        }
        if(!TextUtils.isEmpty(bbs.getRe_num())){
            holder.reply.setText(bbs.getRe_num());
        }
        if(!TextUtils.isEmpty(bbs.getAge_str())){
            holder.state.setText(bbs.getAge_str());
        }
        return view;
    }
    private class ViewHolder{
        ImageView icon;
        ImageView jian;
        ImageView haspic;
        TextView title;
        TextView nickName;
        TextView time;
        TextView reply;
        TextView state;
    }
}
